package com.uiotsoft.small.host.sampler;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import com.uiotsoft.small.mqtt.MqttClientOld;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpPostSyncSampler extends AbstractJavaSamplerClient {
    private String method;
    private String data;
    private HostHttpSampler httpSampler;
    private String topic;
    private MqttClientOld clientOld;
    private static final Logger log = LoggerFactory.getLogger(HttpPostSyncSampler.class);

    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("gateway", "${gateway}");
        params.addArgument("appKey", "${appkey}");
        params.addArgument("appSecret", "${appsecret}");
        params.addArgument("method", "");
        params.addArgument("data", "");
        params.addArgument("topic", "");
        return params;
    }

    private void setupValues(JavaSamplerContext context) {
        this.method = context.getParameter("method");
        this.data = context.getParameter("data");
        topic = context.getParameter("topic");
        String appKey = context.getParameter("appKey");
        String appSecret = context.getParameter("appSecret");
        String gateway = context.getParameter("gateway");

        this.httpSampler = HostHttpSampler.getInstance(appKey, appSecret, gateway);
    }

    public SampleResult runTest(JavaSamplerContext context) {
        this.setupValues(context);
        JMeterVariables vars = JMeterContextService.getContext().getVariables();
        clientOld = (MqttClientOld) vars.getObject("clientOldHost");
        SampleResult results = new SampleResult();
        results.setDataEncoding("UTF-8");
        try {
            results.sampleStart();
            String result = this.httpSampler.hostRequestPost(this.method, this.data);
            String topic;
            if (clientOld != null){
                while (true){
                    topic = clientOld.getTopic();
                    if (topic != null){
                        if (topic.equals(this.topic)){
                            log.info("\n******客户端接收消息成功……\n******clientId：{}\n******topic：{}:\n******content{}",clientOld.getClientId(),topic,result);
                            results.setSuccessful(true);
                            results.setResponseData(clientOld.getResult(), "UTF-8");
                            results.setResponseCodeOK();
                            break;
                        }
                    }
                }
            }else {
                results.setSuccessful(false);
                results.setResponseData("client is null", "UTF-8");
                results.setResponseCode("500");
            }
        } catch (Exception var7) {
            results.setSuccessful(false);
            var7.printStackTrace();
        } finally {
            results.sampleEnd();
        }

        return results;
    }

    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }
}
